﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Newtonsoft.Json;
using System.IO;
using Newtonsoft.Json.Linq;

namespace API_Test_KashFlow
{
    public class GetJsonAPI
    {
        // método para listar quartos
        public void listarQuartos()
        {
            string url = "http://192.168.160.27:8080/WebApplication1/listarQuartos";
            string json = new WebClient().DownloadString(url);
            Room[] rooms = JsonConvert.DeserializeObject<Room[]>(json);
            for (int i = 0; i < rooms.Length; i++)
            {
                Console.WriteLine("Quarto #"+i);
                Console.WriteLine("\tNome: "+rooms[i].name);
                Console.WriteLine("\tPreço: " + rooms[i].price);
                Console.WriteLine("\tDescrição: " + rooms[i].description);
            }
        }

        // método para receber token do Rent-a-Car
        public string receberTokenRentaCar()
        {
            var httpWebRequest = (HttpWebRequest) WebRequest.Create("http://192.168.160.123:8080/Rent-a-Car/Session/Login");
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"username\":\"aviacao\","+"\"password\":\"123456789\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd(); // variável result possui o token
                    JObject o = JObject.Parse(result);
                    return (string) o["token"];
                }
            }
        }

        // método para listar carros entre datas
        public void listarCarrosEntreDatas(string startDate, string endDate)
        {
            string url = "http://192.168.160.123:8080/Rent-a-Car/Cars/Available?token=bb034a42-15a0-4107-b6b0-93de7fa2b5d8&beginDate=" + startDate + "&endDate=" + endDate;
            string json = new WebClient().DownloadString(url);
            Car[] cars = JsonConvert.DeserializeObject<Car[]>(json);
            for (int i = 0; i < cars.Length; i++)
            {
                Console.WriteLine("Carro #" + i);
                Console.WriteLine("\tID: " + cars[i].id);
                Console.WriteLine("\tMarca: " + cars[i].manufacturer);
                Console.WriteLine("\tModelo: " + cars[i].model);
                Console.WriteLine("\tNome: " + cars[i].car);
                Console.WriteLine("\tNome: " + cars[i].image);
            }
        }

        // método para alugar um carro
        public string alugarCarro(int carID, string startDate, string endDate)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://192.168.160.123:8080/Rent-a-Car/Cars/Rent?token=bb034a42-15a0-4107-b6b0-93de7fa2b5d8");
            httpWebRequest.ContentType = "text/json";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{\"carID\":\""+carID+"\"," + "\"nif\":\"123456789\",\"beginDate\":\""+startDate+",\"endDate\":\""+endDate+"\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    JObject o = JObject.Parse(result);
                    return (string) o["invoice"];
                }
            }
        }

        // método para reservar um quarto
        public void reservarQuarto(int idCategoria, int quantidade, string dataInicio, string datafim, string firstName, 
            string lastname, string mail, string telefone)
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://192.168.160.27:8080/WebApplication1/fazerReserva?idCategoria="+idCategoria+
                "&quantidade="+quantidade+"&dateInit="+dataInicio+"&dateEnd="+datafim+"&firstName="+firstName+
                "&lastName="+lastname+"&email="+mail+"&telefone="+telefone);
            httpWebRequest.Method = "POST";

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var result = streamReader.ReadToEnd();
                JObject o = JObject.Parse(result);
                Console.WriteLine("Nº Reserva: " + o["Nr. Reserva"]);
                Console.WriteLine("Nº Noites: " + o["Nr. Noites"]);
                Console.WriteLine("Nome Quarto: " + o["Nome Quarto"]);
                Console.WriteLine("Preço Total: " + o["Preco Total"]);
            }
        }
    }
}
